package com.fanghaoran.notes.dao.impl;

import com.fanghaoran.notes.dao.UserDao;
import com.fanghaoran.notes.entity.User;
import com.fanghaoran.notes.util.BaseDaoUtil;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/04/13/20:30
 * @Description:
 */
public class UserDaoImpl implements UserDao {

    @Override
    public List<User> selectUserByNameAndPwd(User user) throws Exception {
        String sql="select * from user where username=? and password=?";
        return BaseDaoUtil.executeQuery(User.class, sql, user.getUsername(),user.getPassword());
    }

    @Override
    public List<User> selectUserByNameOrEmail(User user) throws Exception {
        String sql="select * from user where username=? or email=?;";
        return BaseDaoUtil.executeQuery(User.class,sql,user.getUsername(),user.getEmail());
    }

    @Override
    public List<User> selectUserByUid(int uId) throws Exception {
        String sql="select * from user where u_id =?;";
        return BaseDaoUtil.executeQuery(User.class,sql,uId);
    }

    @Override
    public Integer insertUser(User user) throws SQLException {
        String sql="insert into user(username,password,salt,email,head_photo,is_merchant,register_time) values(?,?,?,?,?,?,?);";
        return BaseDaoUtil.executeUpdate(sql,user.getUsername(),user.getPassword(),user.getSalt(),
                user.getEmail(),user.getHeadPhoto(),user.getIsMerchant(),user.getRegisterTime());
    }

    @Override
    public Integer updatePwdByEmail(User user) throws SQLException {
        String sql="update user set password=? where email=?;";
        return BaseDaoUtil.executeUpdate(sql,user.getPassword(),user.getEmail());
    }

    @Override
    public Integer updateHeadPhoto(int uId,String filename) throws SQLException {
        String sql="update user set head_photo=? where u_id=?";
        return BaseDaoUtil.executeUpdate(sql,filename,uId);
    }

    @Override
    public Integer updateBalance(int uId,BigDecimal remain) throws SQLException {
        String sql="update user set balance=? where u_id=?;";
        return BaseDaoUtil.executeUpdate(sql,remain,uId);
    }
}
